import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Layout from "../layout/layout.vue";
import Login from "../views/login.vue";
import { useUserStore } from "../pinia/user";
import NotFound from "@/views/notFound.vue";

export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/manager",
    redirect: "/manager/managerList",
    component: Layout,
    meta: {
      title: "账号管理",
    },
    children: [
      {
        path: "/manager/managerList",
        component: () => import("../views/manager/managerList.vue"),
        meta: {
          title: "管理员列表",
        },
      },
      {
        path: "/manager/userList",
        component: () => import("../views/manager/userList.vue"),
        meta: {
          title: "用户列表",
        },
      },
    ],
  },
  {
    path: "/banner",
    redirect: "/banner/bannerList",
    component: Layout,
    meta: {
      title: "轮播图管理",
    },
    children: [
      {
        path: "/banner/bannerList",
        component: () => import("../views/banner/bannerList.vue"),
        meta: {
          title: "轮播图列表",
        },
      },
    ],
  },
  {
    path: "/goods",
    redirect: "/goods/goodsList",
    component: Layout,
    meta: {
      title: "商品管理",
    },
    children: [
      {
        path: "/goods/goodsList",
        component: () => import("../views/goods/goodsList.vue"),
        meta: {
          title: "商品列表",
        },
      },
    ],
  },
  {
    path: "/order",
    redirect: "/order/order",
    component: Layout,
    meta: {
      title: "订单管理",
    },
    children: [
      {
        path: "/order/order",
        component: () => import("../views/order/order.vue"),
        meta: {
          title: "订单列表",
        },
      },
    ],
  },
  {
    path: "/cart",
    redirect: "/cart/cart",
    component: Layout,
    meta: {
      title: "购物车管理",
    },
    children: [
      {
        path: "/cart/cart",
        component: () => import("../views/cart/cart.vue"),
        meta: {
          title: "购物车",
        },
      },
    ],
  },
  {
    path: "/address",
    redirect: "/address/address",
    component: Layout,
    meta: {
      title: "地址管理",
    },
    children: [
      {
        path: "/address/address",
        component: () => import("../views/address/address.vue"),
        meta: {
          title: "地址列表",
        },
      },
    ],
  },
  {
    path: "/data",
    redirect: "/data/data",
    component: Layout,
    meta: {
      title: "数据管理",
    },
    children: [
      {
        path: "/data/data",
        component: () => import("../views/data/data.vue"),
        meta: {
          title: "数据展示",
        },
      },
    ],
  },
  {
    path: "/map",
    redirect: "/map/map",
    component: Layout,
    meta: {
      title: "地图路线",
    },
    children: [
      {
        path: "/map/map",
        component: () => import("../views/map/map.vue"),
        meta: {
          title: "路线图",
        },
      },
    ],
  },
];

export const staticRoutes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/home",
    component: Layout,
    meta: {
      title: "首页",
    },
    children: [
      {
        path: "/home",
        component: () => import("../views/home.vue"),
        meta: {
          title: "首页",
        },
      },
    ],
  },
  {
    path: "/login",
    component: Login,
    name: "login",
  },
  {
    path: "/:pathMatch(.*)*",
    component: NotFound,
    name: "notFound",
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes: [...asyncRoutes, ...staticRoutes],
});

router.beforeEach((to) => {
  const userStore = useUserStore();
  if (!userStore.token && to.name !== "login") {
    return { path: "/login" };
  }
});

// app.use(router);
export default router;
